<template>
	<view class="index ">
		<!--简洁 -->
		<view class="f-1 bff1 box" style="margin: 10px;">
			<u--form labelPosition="left" :model="form" ref="uForm">
				<u-form-item label="点单品上下架:" prop="name" ref="item1" labelWidth="100px" :required="true" borderBottom v-if="type==3">
					<u-radio-group v-model="form.radioSx" class="ml10">
						<u-radio :name="1" shape="circle">上架</u-radio>
						<u-radio :name="2" shape="circle" class="ml10">下架</u-radio>
					</u-radio-group>
				</u-form-item>
				<view v-if="form.sku_list.length>0" class="title">规格信息</view>
				<scroll-view class="scroll f-1 bf8 db" scroll-y="true" v-if="form.sku_list.length>0">
					<view v-for="(item,i) in form.sku_list" :key="i">
						<view class="flex" >
							<u-tag mode="light" :text="item.name" type="primary" plain />
							<u-radio-group v-model="item.radioSx" class="ml10" v-if="type==3">
								<u-radio :name="1" shape="circle">上架</u-radio>
								<u-radio :name="2" shape="circle" class="ml10">下架</u-radio>
							</u-radio-group>
						</view>
						<view class="flex">
							<view class=" juzong">
								商品总价(元)：
								<u--input placeholder="请输入商品总价" border="none" type="number" v-model="item.origin_amount"
									placeholderClass="init" disabled
									:customStyle="{padding:' 15px 10px ',background:'#f4f4f4',borderRadius:'16rpx'}"></u--input>
							</view>
							<view class=" juzong">
								团购价(元)：
								<u--input placeholder="请输入团购价" border="none" type="number" v-model="item.actual_amount"
									placeholderClass="init" :disabled="type>=2"
									:customStyle="{padding:' 15px 10px ',background:type>=2?'#f4f4f4':'#fff',borderRadius:'16rpx'}"></u--input>
							</view>
							<view class=" juzong" v-if="type>=2">
								库存：
								<u--input placeholder="请输入库存" border="none" type="number" v-model="item.stock"
									placeholderClass="init" :disabled="type==3"
									:customStyle="{padding:' 15px 10px ',background:type==3?'#f4f4f4':'#fff',borderRadius:'16rpx'}"></u--input>
							</view>
						</view>
						<u-divider></u-divider>
					</view>
				</scroll-view>
			</u--form>
		</view>

		<!-- 按钮 -->
		<view class="mt10 guding">
			<u-button type="primary" :loading="loading" :customStyle="{color:'#000'}" :plain="true" text="确定"
				color="#0055ff" @click="submit()">
			</u-button>
		</view>
	</view>
</template>

<script>
	import mgCell from '@/components/common/mg-cell.vue'
	import wPicker from "@/components/form/w-picker.vue";
	import category from "@/components/category/category.vue";
	import config from '@/custom/config.js';
	import site from '@/custom/siteroot.js';
	export default {
		components: {
			mgCell,
			wPicker,
			category
		},
		data() {
			return {
				form: {
					place_id:'',
					sku_list: [],
					radioSx:1
				},
				menlist: [],
				jialiaolist: [],
				spulist: [],
				nowDate: '',
				rules: {
				},
				addAndEditType: 0,
				info_id: '',
				loading: false,
				hexList: [],
				type:1,
				adminInfo:{}
			}
		},
		onLoad(option) {
			this.info_id = option.id;
			this.type=option.type;
			this.adminInfo=uni.getStorageSync('adminInfo');
			this.getInfo();
			for (let i = 0; i <= 15; i++) {
				this.hexList[i] = i.toString(16)
			}
		
		},
		watch: {

		},
		methods: {
			async getInfo() {
				let {
					code,
					data,
					msg
				} = await this.beg.request({
					url: '/admin/product.Place/getInfo',
					method: 'POST',
					data: {
						id: this.info_id,
					},
				})
				if (code == 1) {
					let stock=0;
					data.pois.forEach((v) => {
						if(this.adminInfo.store_id==v.local_store_id){
							stock=v.stock;
							return;
						}
					})
					data.sku_list.forEach((v) => {
						v.actual_amount = Number(v.actual_amount) / 100;
						v.origin_amount = Number(v.origin_amount) / 100;
						v.stock=stock;
						v.radioSx=1;
					})
					Object.assign(this.form, data);

				} else {
					uni.$u.toast(msg)
				}
			},

			submit() {
				//检查规格
				let status = false;
				this.form.sku_list.forEach((v) => {
					if (v.origin_amount == 0) {
						status = true;
						return;
					}
					if (v.actual_amount == 0) {
						status = true;
						return;
					}
					if (v.actual_amount > v.origin_amount) {
						status = true;
						return;
					}
				})
				if (status) {
					uni.$u.toast('请选择规格是否正确（不能出现0或者团购价大于商品总价）')
				}

					let data = {
						  place_id: this.form.id,
						  sku_data: [],
						  sj_status:this.form.radioSx
					};
					this.form.sku_list.forEach((v) => {
					        data.sku_data.push({
					            place_sku_id: v.id,
					            store_id: this.adminInfo.store_id,
					            actual_amount: v.actual_amount * 100,
								sj_status:v.radioSx
					        });
					})
					let url='';
					if(this.type==1){
						url='/admin/product.Place/skuStorePrice'
					}
					if(this.type==2){
						url='/admin/product.Place/skuStoreStock'
					}
					if(this.type==3){
						url='/admin/product.Place/skuStoreOperate'
					}
					this.postData(data,url);
			},
			async postData(data,url) {
				this.loading = true;
				let {
					code,
					msg
				} = await this.beg.request({
					url: url,
					method: 'POST',
					data: data,
				})
				if (code == 1) {
					uni.$u.toast(msg);
					setTimeout(() => {
						this.returnIndex();
					}, 1000)
				} else {
					this.loading = false;
					uni.$u.toast(msg)
				}
				this.loading = false;
			},
			//返回
			returnIndex() {
				this.loading = false;
				uni.navigateBack({
					delta: 1
				});
			},
			/**
			 * 生成全球唯一标识
			 * @returns uuid
			 */
			uuid() {
				let uuid = ''
				for (let i = 1; i <= 36; i++) {
					if (i === 9 || i === 14 || i === 19 || i === 24) {
						uuid += '-'
					} else if (i === 15) {
						uuid += 4
					} else if (i === 20) {
						uuid += this.hexList[(Math.random() * 4) | 8]
					} else {
						uuid += this.hexList[(Math.random() * 16) | 0]
					}
				}
				return uuid
			},
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		background-color: #fff;
		padding: 20rpx;
		line-height: 1.5;
		position: relative;
		margin-top: 20rpx;
		border-radius: 20rpx;



		.prid {
			font-weight: bold;
			font-size: 32rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.jiao {
				right: 10rpx;

				.jiaohang {
					display: flex;
					align-items: center;

					.rijianju {
						margin-right: 10rpx;
					}
				}
			}
		}

		.prod {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 20rpx;
		}

		.prname {
			width: 80%;
			overflow: hidden;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
		}

		.price {
			width: 20%;
			text-align: right;
		}
	}

	.scroll {
		height: 50vh;
		background-color: #fff;
	}

	/deep/.u-form-item__body {
		padding: 0px;
	}

	.zanwei {
		height: 100rpx;
		display: flex;
		align-items: center;
	}

	.juzong {
		align-items: center;
		margin-top: 10rpx;
	}

	.title {
		font-weight: bold;
		padding: 20rpx;
	}
	/deep/ .u-radio-group--row{
		display: flex;
	}
	/deep/ .u-form-item__body{
		height: 80px;
	}

	// .guding{
	// 	position: fixed;
	// 	bottom: 0rpx;
	// 	width: 100%;
	// 	padding: 20rpx;
	// }
</style>